Strict

Import agfx

Global myAstarScreen:TAstarScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		'DEVICE.SetFps(30)		' default FPS is 60
		
		Super.OnCreate()
				
		'DISPLAY.SetVirtualSize(480,320)
		myAstarScreen = New TAstarScreen		
		myAstarScreen.Prepare()
		
		Return 0
	End
	
	
End


Class TAstarScreen Extends TScreen
	
	Field testMAP:Float[][]
	Field PStart:TVector2
	Field PEnd:TVector2


	' START when app is created
	Method Start:Void()



		Self.PStart = New TVector2(1,1)
		Self.PEnd = New TVector2(49,49)

		Self.testMAP = FloatArray2D(50,50)
		Self.MakeMap()
		
	End
	
	
	Method Update:Void()

		If KeyHit(KEY_1) Then
			TPathfinder.SetUp(testMAP,50,50,0)
			Print("0=no diagonal movement")							
		End 

		If KeyHit(KEY_2) Then
			TPathfinder.SetUp(testMAP,50,50,1)			
			Print("1=diagonal movement and cutting corners allowed")			
		End 

		If KeyHit(KEY_3) Then
			TPathfinder.SetUp(testMAP,50,50,2)
			Print("2=diagonal movement but no cutting corners")
		End 

		If MOUSE.Hit() Then
			Self.PStart.Set(MOUSE.x/10,MOUSE.y/10)
			Self.PEnd.Set(MOUSE.x/10,MOUSE.y/10)
			
		End

		If MOUSE.Down() Then
			
			Self.PEnd.Set(MOUSE.x/10 ,MOUSE.y/10)

		End

		If MOUSE.Up() Then
			
			Self.PEnd.Set(MOUSE.x/10 ,MOUSE.y/10)
			TPathfinder.FindPath(Self.PStart,Self.PEnd)
			
		End


		If KeyHit(KEY_SPACE) Then
			TPathfinder.FindPath(Self.PStart,Self.PEnd)			
		End

	End
	
	
	Method Render:Void()
		Cls(0,0,0)
		
		For Local I% = 0 Until 50
			For Local J% = 0 Until 50
				SetAlpha( Min(testMAP[I][J],1.0))
				DrawRect(Int(I*10),Int(J*10),9,9)
			Next
		Next	
		
		SetAlpha(1)
		SetColor(255,0,0)
		For Local Z% = 0 Until TPathfinder.Route.Length Step 2
			DrawRect(TPathfinder.Route[Z]*10, TPathfinder.Route[Z+1]*10,9,9)
		Next

		
		DrawLine(3+Self.PStart.x*10,3+Self.PStart.y*10,3+Self.PEnd.x*10,3+Self.PEnd.y*10)
		SetColor(255,0,0)
		DrawOval(3+Self.PStart.x*10,3+Self.PStart.y*10,4,4)
		SetColor(0,255,0)
		DrawOval(3+Self.PEnd.x*10,3+Self.PEnd.y*10,4,4)

		SetColor(255,255,255)
		
		DrawText("[1-3] - path mode",640-130,0)
		DrawText("[SPACE] - calc path",640-130,15)

	End Method


	Method MakeMap:Void()
		For Local I:Int = 0 Until 50
			For Local J:Int = 0 Until 50
				testMAP[I][J] = Rnd(0,1.5)
			Next
		Next	
		
		TPathfinder.SetUp(testMAP,50,50) 

	End Method


End


